/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.mobbuzz.page.menu.inbox;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.plaf.Border;
import edu.mobbuzz.bean.Message;
import edu.mobbuzz.facade.MessageFacade;
import edu.mobbuzz.messaging.RequestStream;
import edu.mobbuzz.page.menu.Menu;
import edu.mobbuzz.page.menu.outbox.OutboxPage;
import edu.mobbuzz.util.string.MenuString;
import edu.mobbuzz.util.string.ModeString;
import java.util.Vector;

/**
 *
 * @author dailicious
 */
public class RecepientListPage extends Menu {
    private Form recepientForm;
    private Container recepientPanel;
    private Label menuTitle;
    private Label toLbl;
    private List recepientList;
    private DefaultListModel recepientModel;
    private Vector recepientArr;
    private Message message;
    private Command sendCmd;
    private Command addRecepientCmd;
    private Command removeRecepientCmd;
    private Command backCmd;
    private int mode;
    private int messageType;
    private MessageFacade messageFacade;
    
    public RecepientListPage(){
        recepientForm = new Form();
        recepientPanel = new Container();
        menuTitle = new Label(getMenuTitle());
        toLbl = new Label();
        
       

        
        sendCmd = new Command("Send"){
            public void actionPerformed(ActionEvent evt) {
                sendMessage();
            }
        };

        addRecepientCmd = new Command("Add Recepient"){
            public void actionPerformed(ActionEvent evt) {
                backToRecepientPage();
            }
        };

        removeRecepientCmd = new Command("Remove Recepient"){
            public void actionPerformed(ActionEvent evt) {
                removeRecepient();
            }
        };

        backCmd = new Command("Back"){
            public void actionPerformed(ActionEvent evt) {
                backToRecepientPage();
            }
        };
    }

    public Vector getRecepientArr() {
        if(recepientArr==null){
            recepientArr = new Vector();
        }
        return recepientArr;
    }


    public void setRecepientArr(Vector recepientArr) {
        this.recepientArr = recepientArr;
    }

    public Message getMessage() {
        return message;
    }


    public void setMessage(Message message) {
        this.message = message;
    }

    public int getMode() {
        return mode;
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    public int getMessageType() {
        return messageType;
    }


    public void setMessageType(int messageType) {
        this.messageType = messageType;
    }
    
    public void showPage() {
        for (int i=0; i< getRecepientArr().size(); i++) {
            String _recepient= (String) getRecepientArr().elementAt(i);
            System.out.println(_recepient);

        }

        recepientModel = new DefaultListModel(getRecepientArr());
        recepientList = new List(recepientModel);
        recepientList.getStyle().setMargin(2, 0, 10, 10);
        recepientList.getStyle().setBorder(Border.getEmpty());
        
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);

        toLbl.setText("To :");
        toLbl.getStyle().setMargin(2, 0, 10, 10);

        recepientPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        recepientPanel.addComponent(menuTitle);
        recepientPanel.addComponent(toLbl);
        recepientPanel.addComponent(recepientList);

        recepientForm.setLayout(new BorderLayout());
        recepientForm.addComponent(BorderLayout.CENTER, recepientPanel);

        recepientForm.addCommand(backCmd);
        if(recepientModel.getSize()!=0){
            recepientForm.addCommand(removeRecepientCmd);
        }
        
        recepientForm.addCommand(addRecepientCmd);

        if(recepientModel.getSize()!=0){
            recepientForm.addCommand(sendCmd);
        }
        
        recepientForm.setCommandListener(getActionlistener());
        recepientForm.show();
    }

    public void sendMessage(){
        if(getMode() == ModeString.EDIT){
             System.out.println(">> EDIT");
            StringBuffer request = RequestStream.sendNews(getMessage().getSource(), getMessage().getLink(), getMessage().getTitle(), getMessage().getDescription(), getMessage().getDate());
            for(int i=0;i<getRecepientArr().size();i++){
                String to = (String)getRecepientArr().elementAt(i);
                System.out.println("send to : " +to+"@mobbuzz.com");
                getMessaging().sendMessage(to+"@mobbuzz.com",request.toString());
            }

        }else if(getMode() == ModeString.NEW){
             System.out.println(">> NEW");
            for(int i=0;i<getRecepientArr().size();i++){
                String to = (String)getRecepientArr().elementAt(i);
                System.out.println("send to : " +to+"@mobbuzz.com");
                getMessaging().sendMessage(to+"@mobbuzz.com",getMessage().getDescription());
            }
        }
        getMessageFacade().addOutbox(getMessage());
        goToOutboxPage();
    }

    

    public void removeRecepient(){
        String recepient = (String)recepientList.getSelectedItem();        
        getRecepientArr().removeElement(recepient);
        recepientModel.removeItem(recepientList.getSelectedIndex());
        if(recepientModel.getSize()==0){
            recepientForm.removeCommand(removeRecepientCmd);
            recepientForm.removeCommand(sendCmd);
        }
    }
    public void backToRecepientPage(){
        RecepientPage contactPage = new RecepientPage();
        contactPage.setExitCmd(getExitCmd());
        contactPage.setActionlistener(getActionlistener());
        contactPage.setMessage(getMessage());
        contactPage.setMode(getMode());
        contactPage.setMessaging(getMessaging());
        contactPage.setRecepientArr(getRecepientArr());
        contactPage.showPage();
    }

    public void goToOutboxPage(){
        OutboxPage outboxPage = new OutboxPage();
        outboxPage.setExitCmd(getExitCmd());
        outboxPage.setMessaging(getMessaging());
        outboxPage.setActionlistener(getActionlistener());
        outboxPage.showPage();
    }

    public MessageFacade getMessageFacade() {
        if(messageFacade==null){
            messageFacade = new MessageFacade();
        }
        return messageFacade;
    }
    
    public String getMenuTitle() {
        return MenuString.SEND_MESSAGE;
    }

    

}
